#include <QtTest>

#include "../tests_common.h"

#include "common.h"
#include "bit_range.h"
#include "row_text.h"
#include "rows.h"


using namespace BitWin;


class TestWordRow: public QObject
{

    Q_OBJECT

private slots:

    void test_data();
    void test();
};


void TestWordRow::test_data()
{
    QTest::addColumn<QString>("word_title");
    QTest::addColumn<int>("word_no");
    QTest::addColumn<QString>("indata");
    QTest::addColumn<BitRange>("range");
    QTest::addColumn<PRowText>("outdata_expected");
    
    QTest::newRow("8 of 8")
        << "Row #1" << 1
        << "abcdefgh" << BitRange(0, 8) << STRING_TEXT("abcdefgh")
    ;
    QTest::newRow("[0, 3)")
        << "Row #2" << 2
        << "abcdefgh" << BitRange(0, 3) << STRING_TEXT("abc")
    ;
    QTest::newRow("[3, 6)")
        << "Row #3" << 3
        << "abcdefgh" << BitRange(3, 6) << STRING_TEXT("def")
    ;
    QTest::newRow("Null range")
        << "Row #4" << 14
        << QString("abcdefgh") << BitRange() << STRING_TEXT(QString())
    ;
    QTest::newRow("Range end exceeds string end")
        << "Row #5" << 15
        << QString("abcdefgh") << BitRange(3, 10) << STRING_TEXT("defgh  ")
    ;
    QTest::newRow("Range begin exceeds string end")
        << "Row #6" << 16
        << QString("abc") << BitRange(3, 6) << STRING_TEXT("   ")
    ;
}

void TestWordRow::test()
{
    QFETCH(QString, word_title);
    QFETCH(int, word_no);
    
    WordRow row(word_title, word_no);
    QCOMPARE(row.title(), word_title);
    QCOMPARE(row.wordNo(), word_no);
    
    QFETCH(QString, indata);
    QFETCH(BitRange, range);
    
    QVERIFY(
        !row
            .data(Qt::DisplayRole, range, STRING_TEXT(indata))
            .isValid()
    );
    QVERIFY(
        !row
            .data(BitwiseDataRole + 1, range, STRING_TEXT(indata))
            .isValid()
    );
    
    QVariant data(
        row.data(BitwiseDataRole, range, STRING_TEXT(indata))
    );
    QVERIFY(data.isValid());
    QVERIFY(data.canConvert<PRowText>());
    PRowText outdata(data.value<PRowText>());
    
    QFETCH(PRowText, outdata_expected);
    QCOMPARE(outdata, outdata_expected);
}


QTEST_MAIN(TestWordRow)


#include "main.moc"
